//
//  XHInputItem.swift
//  YunshuApp
//
//  Created by 周文杰 on 2017/6/9.
//  Copyright © 2017年 zwj. All rights reserved.
//

import UIKit

typealias onTapListener = (XHInputItem)->Void

class XHInputItem: UIView {

    let nameLabel = UILabel.init(frame: CGRect.init(x: 10, y: 0, width: 100, height: 46))
    let topLine = UIView.init(frame: CGRect.init(x: 0, y: 0, width: Const.SCREEN_WIDTH, height: Const.ONE_PIXEL))

    let bottomLine = UIView.init(frame: CGRect.init(x: 0, y: 46 - Const.ONE_PIXEL, width: Const.SCREEN_WIDTH, height: Const.ONE_PIXEL))
    
    let vseperator = UIView.init(frame: CGRect.init(x: 100, y: 8, width: Const.ONE_PIXEL, height: 30 ))
    
    let valueField = UITextField.init(frame: CGRect.init(x: 105, y: 0, width: Const.SCREEN_WIDTH - 105, height: 46))
    let valueLabel = UILabel.init(frame: CGRect.init(x: 105, y: 0, width: Const.SCREEN_WIDTH - 105, height: 46))
    let arrowImg = UIImageView.init(frame: CGRect.init(x: Const.SCREEN_WIDTH - 35, y: 10, width: 30, height: 30))
    
    let thumbnail = UIImageView.init(frame: CGRect.init(x: Const.SCREEN_WIDTH - 70, y: 10, width: 30, height: 30))
    
    var callback : onTapListener?
    
    var hint = ""
    
    
    init(_ name:String, placeHolder:String="", onTap:onTapListener? ) {
        super.init(frame: CGRect.init(x: 0, y: 0, width: Const.SCREEN_WIDTH, height: 46))
        self.backgroundColor = UIColor.white
        callback = onTap
        hint = placeHolder
        
        nameLabel.text = name
        nameLabel.font = UIFont.systemFont(ofSize: 12)
        nameLabel.textColor = Const.WTM_GRAY
        self.addSubview(nameLabel)
        
        topLine.backgroundColor = Const.WTM_LIGHTGRAY
        bottomLine.backgroundColor = Const.WTM_LIGHTGRAY
        vseperator.backgroundColor = Const.WTM_LIGHTGRAY
        self.addSubview(topLine)
        self.addSubview(bottomLine)
        self.addSubview(vseperator)
        
        if onTap == nil {
            valueField.placeholder = placeHolder
            valueField.font = UIFont.systemFont(ofSize: 12)
            self.addSubview(valueField)
        }else {
            
            valueLabel.text = placeHolder
            valueLabel.font = UIFont.systemFont(ofSize: 12)
            self.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(XHInputItem.onTap)))
            self.addSubview(valueLabel)
        }
        
        arrowImg.image = UIImage.init(named: "right_arrow")
        arrowImg.isHidden = (onTap == nil)
        self.addSubview(arrowImg)

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setY(_ y:CGFloat) {
        self.frame = CGRect.init(x: 0, y: y, width: Const.SCREEN_WIDTH, height: 46)

    }
    
    func showImage() {
        thumbnail.backgroundColor = Const.WTM_LIGHTGRAY
        thumbnail.contentMode = .scaleAspectFill
        thumbnail.layer.masksToBounds = true
        self.addSubview(thumbnail)
    }
    
    @objc func onTap() {
        if let f = self.callback {
            f(self)
        }
    }
    
    func getText() -> String? {
        
        if self.valueField.superview != nil || self.thumbnail.superview != nil{
            return self.valueField.text
        }
        
        if self.valueLabel.text == self.hint {
            return ""
        }else {
            return self.valueLabel.text
        }
    }
    
    func setText(_ txt:String) -> Void {
        if self.valueField.superview != nil{
            self.valueField.text = txt
        }else{
            self.valueLabel.text = txt
        }
    }

}

